//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.wechat.login.controller;

import cn.hutool.json.JSONUtil;
import com.example.wechat.login.dto.ApiResponse;
import com.example.wechat.login.dto.WechatLoginRequest;
import com.example.wechat.login.dto.WechatLoginResponse;
import com.example.wechat.login.service.WechatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/wechat"})
public class WechatController {
    private static final Logger logger = LoggerFactory.getLogger(WechatController.class);
    private final WechatService wechatService;

    public WechatController(WechatService wechatService) {
        this.wechatService = wechatService;
    }

    @PostMapping({"/login"})
    public ApiResponse<WechatLoginResponse> login(@RequestBody WechatLoginRequest loginRequest) {
        logger.info("接收到微信登录请求：{}", JSONUtil.toJsonStr(loginRequest));

        try {
            WechatLoginResponse response = this.wechatService.login(loginRequest);
            return ApiResponse.success(response);
        } catch (Exception e) {
            logger.error("微信登录失败", e);
            return ApiResponse.error("登录失败: " + e.getMessage());
        }
    }

    @GetMapping({"/check"})
    public ApiResponse<String> checkLogin() {
        return ApiResponse.success("已登录");
    }
}
